If your program needs to choose several options from many possibilities, can this structure be used?

Answer:

No, not easily. IF-ELSEIF is a specialist for choosing one out of many.

Airfare Program Redone with IF-ELSEIF

Remember the problem that the Airfare Program was to solve:

  1. Children under 12 fly free.
  2. Individuals from 12 to 23 fly at student rate, 70% of full fare.
  3. Adults 24 through 64 pay full fare.
  4. Seniors 65 and older pay 75% of full fare.
airfare flow chart with ELSEIFs

This is a choice of one of four options and fits well with what IF-ELSEIF does. Here is the program re-written:

 
PRINT "Enter full fare"
INPUT FARE
PRINT "Enter age"
INPUT AGE
'
'Calculate discount based on age

IF AGE < 12 THEN 
  LET RATE = 0.0 

ELSEIF AGE  < 24 THEN
  LET RATE = 0.70 

ELSEIF  AGE < 65 THEN
  LET RATE = 1.0
   
ELSE
  LET RATE = 0.75

END IF

'Calculate fare
PRINT "Your fare is:", FARE * RATE
END

QUESTION 12:

Look at the program. What is the fare paid by someone who is 23 years old when the base fare is $100?